//
//  JYPaymentController.m
//  JYFarm
//
//  Created by 花花 on 2017/10/18.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "JYPaymentController.h"
#import "JYRechargeCell.h"
@interface JYPaymentController (){
NSIndexPath *currntPayTypeindex;
}

@property(nonatomic)NSArray *titleArr;

@property(nonatomic)NSMutableArray *dataArr;

@property(nonatomic)NSMutableArray *deldataArr;

@end

static NSString *const SystemPaymentCellID = @"SystemPaymentCellID";
static NSString *const PaymentCellID = @"PaymentCellID";
static NSString *const  MPointMoneyCellID = @"MPointMoneyCellID";


@implementation JYPaymentController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"支付";
//    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] animated:YES scrollPosition:UITableViewScrollPositionTop];
    
    [self setupTableView];
    
    [self loadDataFromNetwork];
    
}

- (void)loadDataFromNetwork{

    NSArray *arr = @[@"美点余额（100.00元)"];
    for (int i = 0; i<arr.count; i++) {
        
        JYPaymentModel *model  =[[JYPaymentModel alloc]init];
        model.title = arr[i];
        model.selected = YES;
        [self.dataArr addObject:model];
    }
  
}
- (void)setupTableView{
    
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:SystemPaymentCellID];
    [self.tableView registerClass:[JYRechargeCell class] forCellReuseIdentifier:PaymentCellID];
    [self.tableView registerClass:[MPointMoneyCell class] forCellReuseIdentifier:MPointMoneyCellID];
    
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, kBtnHeight+30+20)];
    UILabel *bottomLabel = [UILabel labelText:@"注：美点余额可与其他支付方式同时支付" fontSize:12 textColor:k9Color textAlignment:NSTextAlignmentLeft];
    bottomLabel.frame = CGRectMake(10, 5, Screen_Width, 20);
    [footerView addSubview:bottomLabel];
    
    
    UIButton *rechargeBtn  =  [UIButton jy_buttontitle:@"确认支付" titleColor:kfColor frame:CGRectMake(10, CGRectGetMaxY(bottomLabel.frame)+10, Screen_Width-20, kBtnHeight) target:self action:@selector(payOnClick) fontSize:17 bgImageName:@"login_button_background"];
    
    [footerView addSubview:rechargeBtn];
    
    self.tableView.tableFooterView = footerView;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return 1;
    }else if (section == 1){
        
        return [self.dataArr count];
    }
    
    return [self.titleArr count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JYRechargeCell *cell = [tableView dequeueReusableCellWithIdentifier:PaymentCellID];
    if (indexPath.section == 0) {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SystemPaymentCellID];
        
        NSString *tip = @"合计金额: ";
        NSString *money = @"66.00元";
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:tip];
        
        NSDictionary *attributes =  @{
                                      NSFontAttributeName:kFont(13),
                                      NSForegroundColorAttributeName:[UIColor colorWithRed:0.94f green:0.13f blue:0.42f alpha:1.00f]
                                      };
        NSAttributedString *str = [[NSAttributedString alloc]initWithString:money attributes:attributes];
        [attributedStr appendAttributedString:str];
        cell.textLabel.font = kFont(13);
        cell.textLabel.attributedText = attributedStr;
        
        return cell;
    }else if(indexPath.section ==1){
        
        JYWeakSelf;
        MPointMoneyCell *cell = [tableView dequeueReusableCellWithIdentifier:MPointMoneyCellID];
        
        
        cell.selectBtnClick = ^(BOOL isSelected){
            
            JYPaymentModel *model = weakSelf.dataArr[indexPath.row];
            model.selected = isSelected;
        };
        cell.model = [self.dataArr safeObjectAtIndex:indexPath.row];
        return cell;
    }else{
    
        cell.titleLabel.text = [self.titleArr safeObjectAtIndex:indexPath.row];
        return cell;
    }
    return nil;
}

- (NSMutableArray *)dataArr{
    
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 1) {
     
        UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 55)];
        headerView.backgroundColor = kBgColor;
        UILabel *label = [UILabel labelText:@"支付方式" fontSize:13 textColor:k3Color textAlignment:NSTextAlignmentCenter];
        label.backgroundColor = [UIColor whiteColor];
        label.frame = CGRectMake(0, 10, Screen_Width, 45);
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, headerView.height-1, Screen_Width, 1)];
        lineView.backgroundColor = kBgColor;
        [headerView addSubview:label];
        [headerView addSubview:lineView];
        return headerView;
    }
    return  nil;

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"%zd",indexPath.row);

}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 1) {
       return 55;
    }
    return 0;
    
}
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//
////
////    if (section == 0) {
////        return 0;
////    }
////    return 10;
//}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return kTableView_rowHeight;
    
}
- (void)payOnClick{
    
    NSLog(@"payOnClick");
}


- (NSArray *)titleArr{

    if (!_titleArr) {
        _titleArr = @[@"货到付款",@"微信支付",@"支付宝支付",@"银行卡支付"];
    }
    return _titleArr;

}
- (NSMutableArray *)deldataArr{
    
    if (!_deldataArr) {
        _deldataArr = [NSMutableArray array];
    }
    return _deldataArr;
    
}

@end





@interface MPointMoneyCell()


@end


@implementation MPointMoneyCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{


    if (self  = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
    
        [self setupSubView];
    }
    
    
    return self;

}

- (void)layoutSubviews{

    [super layoutSubviews];
    
    JYWeakSelf;
    [self.selBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf);
        make.centerY.mas_equalTo(weakSelf);
        make.width.mas_equalTo(weakSelf);
        make.height.mas_equalTo(50);
    
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.contentView.mas_left).offset(35);
        make.centerY.mas_equalTo(weakSelf);
    }];
    
   
    
     
}
- (void)setupSubView{
  
    [self.contentView addSubview:self.selBtn];

    [self.contentView addSubview:self.titleLabel];

}

- (UILabel *)titleLabel{
    if (!_titleLabel ) {
        _titleLabel =[UILabel labelText:@"" fontSize:13 textColor:k9Color textAlignment:NSTextAlignmentLeft];
    }
    return _titleLabel;

}
- (UIButton *)selBtn{

    if (!_selBtn) {
        _selBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_selBtn setImage:[UIImage imageNamed:@"shopping_cart_select"] forState:UIControlStateNormal];
        [_selBtn setImage:[UIImage imageNamed:@"shopping_cart_select_click"] forState:UIControlStateSelected];
        [_selBtn addTarget:self action:@selector(selOnClick:) forControlEvents:UIControlEventTouchUpInside];
        _selBtn.titleLabel.font= kFont(13);
        [_selBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        [_selBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 20, 0, 0)];
        _selBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
      
    }
    return _selBtn;
}

- (void)selOnClick:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    if (self.selectBtnClick) {
        self.selectBtnClick(sender.selected);
    }
}


- (void)setModel:(JYPaymentModel *)model{
    
    _model = model;
    self.titleLabel.text = model.title;
    
    if (model.selected) {
        self.selBtn.selected = YES;
    }else{
        self.selBtn.selected = NO;
    }
  
    
}
@end
